package com.tianzeculture.stagebao.activity.business;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.WorkTypeActivity;
import com.tianzeculture.stagebao.activity.home.SelectCityActivity;
import com.tianzeculture.stagebao.activity.personal.SelectJobActivity;
import com.tianzeculture.stagebao.application.StageBaoApplication;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.Notice;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.dialog.DateDialog;
import com.tianzeculture.stagebao.dialog.SexThreeDialog;
import com.tianzeculture.stagebao.dialog.UnitDialog;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/4 0004.
 * 发布通告
 */

public class PublishNoticeActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.btnNextStep)
    Button btnNextStep;

    @BindView(R.id.layoutWorkType)
    LinearLayout layoutWorkType;
    @BindView(R.id.layoutWorkStartTime)
    LinearLayout layoutWorkStartTime;
    @BindView(R.id.layoutWorkEndTime)
    LinearLayout layoutWorkEndTime;
    @BindView(R.id.layoutWorkPriceUnit)
    LinearLayout layoutWorkPriceUnit;
    @BindView(R.id.layoutWorkAddress)
    LinearLayout layoutWorkAddress;
    @BindView(R.id.layoutWorkSex)
    LinearLayout layoutWorkSex;

    @BindView(R.id.edtWorkTopic)
    EditText edtWorkTopic;
    @BindView(R.id.tvWorkType)
    TextView tvWorkType;
    @BindView(R.id.tvWorkStartTime)
    TextView tvWorkStartTime;
    @BindView(R.id.tvWorkEndTime)
    TextView tvWorkEndTime;
    @BindView(R.id.tvWorkPriceUnit)
    TextView tvWorkPriceUnit;
    @BindView(R.id.tvWorkAddress)
    TextView tvWorkAddress;
    @BindView(R.id.edtWorkPeopleNum)
    EditText edtWorkPeopleNum;
    @BindView(R.id.edtWorkPeopleBudget)
    EditText edtWorkPeopleBudget;
    @BindView(R.id.tvWorkSex)
    TextView tvWorkSex;
    @BindView(R.id.edtWorkHeight)
    EditText edtWorkHeight;

    long beginDay = 0;
    long endDay = 0;

    boolean recruit;
    NoticeList noticeList;

    @Override
    public void initUI() {
        super.initUI();
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        StageBaoApplication.addActivity(this);
        recruit = getIntent().getBooleanExtra("isRecruit", false);
        noticeList = (NoticeList) getIntent().getSerializableExtra("recruitDetail");
        tvTitle.setText(getString(R.string.publish_notice));
        tvRight.setText(getString(R.string.trust_publish));
        if (recruit) {
            edtWorkTopic.setText(noticeList.getTitle());
//            switch (noticeList.getWorkType()) {
//                case "0":
//                    tvWorkType.setText("商演路演");
//                    break;
//                case "1":
//                    tvWorkType.setText("平面拍摄");
//                    break;
//                case "2":
//                    tvWorkType.setText("会展活动");
//                    break;
//                case "3":
//                    tvWorkType.setText("网络宣传");
//                    break;
//            }

            tvWorkType.setText(noticeList.getWorkType());
            tvWorkStartTime.setText(noticeList.getStarttime());
            tvWorkEndTime.setText(noticeList.getEndtime());
            tvWorkAddress.setText(noticeList.getAddress());
            edtWorkPeopleNum.setText(noticeList.getPeople() + "");
            edtWorkPeopleBudget.setText(noticeList.getUnitPrice() + "");
            tvWorkSex.setText(noticeList.getSex().equals("1") ? "男" : "女");
            edtWorkHeight.setText(noticeList.getHeight());
        }
    }


    @OnClick({R.id.tvRight, R.id.btnNextStep, R.id.layoutWorkType, R.id.layoutWorkStartTime, R.id.layoutWorkEndTime,
            R.id.layoutWorkAddress, R.id.layoutWorkSex,R.id.layoutWorkPriceUnit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvRight://委托平台发布
                Intent intent = new Intent(this, PublishNoticeTwoActivity.class);
                intent.putExtra("oneself", false);
                startActivity(intent);
                break;
            case R.id.btnNextStep://下一步
                if (checkEmpty()) {
                    Intent intent2 = new Intent(this, PublishNoticeTwoActivity.class);
                    if (recruit) {
                        noticeList.setTitle(edtWorkTopic.getText().toString().trim());
                        noticeList.setWorkType(tvWorkType.getText().toString().trim());
                        noticeList.setStarttime(tvWorkStartTime.getText().toString().trim());
                        noticeList.setEndtime(tvWorkEndTime.getText().toString().trim());
                        noticeList.setAddress(tvWorkAddress.getText().toString().trim());
                        noticeList.setPriceType(tvWorkPriceUnit.getText().toString().trim());
                        noticeList.setPeople(Integer.parseInt(edtWorkPeopleNum.getText().toString().trim()));
                        noticeList.setUnitPrice(Integer.parseInt(edtWorkPeopleBudget.getText().toString().trim()));
                        if ("男".equals(tvWorkSex.getText().toString().trim())) {
                            noticeList.setSex("1");
                        } else if ("女".equals(tvWorkSex.getText().toString().trim())) {
                            noticeList.setSex("0");
                        } else {
                            noticeList.setSex("2");
                        }
                        noticeList.setHeight(edtWorkHeight.getText().toString().trim());
                        intent2.putExtra("noticelist", noticeList);
                    } else {
                        Notice notice = new Notice();
                        notice.setWorkName(edtWorkTopic.getText().toString().trim());
                        notice.setWork_type(tvWorkType.getText().toString().trim());
                        notice.setStratTime(tvWorkStartTime.getText().toString().trim());
                        notice.setEndTime(tvWorkEndTime.getText().toString().trim());
                        notice.setWorkarea(tvWorkAddress.getText().toString().trim());
                        notice.setPriceType(tvWorkPriceUnit.getText().toString().trim());
                        notice.setWorknumber(Integer.parseInt(edtWorkPeopleNum.getText().toString().trim()));
                        notice.setWorkmoney(Integer.parseInt(edtWorkPeopleBudget.getText().toString().trim()));
                        if ("男".equals(tvWorkSex.getText().toString().trim())) {
                            notice.setWorkSex(1);
                        } else if ("女".equals(tvWorkSex.getText().toString().trim())) {
                            notice.setWorkSex(0);
                        } else {
                            notice.setWorkSex(2);
                        }
                        notice.setWorkheight(edtWorkHeight.getText().toString().trim());
                        intent2.putExtra("notice", notice);
                    }
                    intent2.putExtra("oneself", true);
                    intent2.putExtra("isRecruit", recruit);
                    startActivity(intent2);
                }
                break;
            case R.id.layoutWorkType://工作类型
                Intent intent1 = new Intent(PublishNoticeActivity.this, SelectJobActivity.class);
                startActivityForResult(intent1, Util.WORK_TYPE);
                break;
            case R.id.layoutWorkStartTime://工作开始时间
                DateDialog dateDialog = new DateDialog(PublishNoticeActivity.this,
                        getString(R.string.start_time), true, 0, endDay);
                dateDialog.setOnSelectClickListener(new DateDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(int year, int month, int day) {
                        tvWorkStartTime.setText(year + "-" + month + "-" + day);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            beginDay = format.parse(tvWorkStartTime.getText().toString()
                                    .trim()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                dateDialog.show();
                break;
            case R.id.layoutWorkEndTime://工作结束时间
                DateDialog dateDialog2 = new DateDialog(PublishNoticeActivity.this,
                        getString(R.string.start_time), true, beginDay, 0);
                dateDialog2.setOnSelectClickListener(new DateDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(int year, int month, int day) {
                        tvWorkEndTime.setText(year + "-" + month + "-" + day);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            endDay = format.parse(tvWorkEndTime.getText().toString()
                                    .trim()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                dateDialog2.show();
                break;
            case R.id.layoutWorkPriceUnit://报价单位
                UnitDialog unitDialog = new UnitDialog(PublishNoticeActivity.this);
                unitDialog.setOnSelectClickListener(new UnitDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(String string) {
                        tvWorkPriceUnit.setText(string);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                unitDialog.show();
                break;
            case R.id.layoutWorkAddress://工作地点
                Intent intent3 = new Intent(PublishNoticeActivity.this, SelectCityActivity.class);
                intent3.putExtra("CITY", tvWorkAddress.getText().toString().trim());
                intent3.putExtra("noEventBus", true);
                startActivityForResult(intent3, Util.SELECT_CITY);
                break;
            case R.id.layoutWorkSex://性别要求
                SexThreeDialog sexDialog = new SexThreeDialog(PublishNoticeActivity.this);
                sexDialog.setOnSelectClickListener(new SexThreeDialog.OnSelectClickListener() {
                    @Override
                    public void onOk(int sex) {
                        if (sex == 0) {
                            tvWorkSex.setText("女");
                        } else if (sex == 1) {
                            tvWorkSex.setText("男");
                        } else {
                            tvWorkSex.setText("不限");
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                });
                sexDialog.show();
                break;
        }
    }

    /**
     * 判断所有值是否输入完全
     */
    private boolean checkEmpty() {
        if ("".equals(edtWorkTopic.getText().toString().trim())) {
            ToastUtil.showShort(this, "工作主题不能为空!");
            return false;
        } else if ("".equals(tvWorkType.getText().toString().trim())) {
            ToastUtil.showShort(this, "请选择工作类型!");
            return false;
        } else if ("".equals(tvWorkStartTime.getText().toString().trim())) {
            ToastUtil.showShort(this, "请选择工作开始时间!");
            return false;
        } else if ("".equals(tvWorkEndTime.getText().toString().trim())) {
            ToastUtil.showShort(this, "请选择工作结束时间!");
            return false;
        } else if ("".equals(tvWorkPriceUnit.getText().toString().trim())) {
            ToastUtil.showShort(this, "请选择报价单位!");
            return false;
        } else if ("".equals(tvWorkAddress.getText().toString().trim())) {
            ToastUtil.showShort(this, "请输入工作地点!");
            return false;
        } else if ("".equals(edtWorkPeopleNum.getText().toString().trim())) {
            ToastUtil.showShort(this, "请输入招募人数!");
            return false;
        } else if ("".equals(edtWorkPeopleBudget.getText().toString().trim())) {
            ToastUtil.showShort(this, "请输入人均预算!");
            return false;
        } else if ("".equals(tvWorkSex.getText().toString().trim())) {
            ToastUtil.showShort(this, "请选择性别要求!");
            return false;
        } else if ("".equals(edtWorkPeopleBudget.getText().toString().trim())) {
            ToastUtil.showShort(this, "请输入身高!");
            return false;
        }
        return true;
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);

    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_publish_notice;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Util.CHOOSE_JOB) {
            Bundle bundle = data.getExtras();
            String workType = bundle.getString("JOB");
            tvWorkType.setText(workType);
        } else if (resultCode == Util.SELECT_CITY) {
            Bundle bundle = data.getExtras();
            String workType = bundle.getString("CITY");
            tvWorkAddress.setText(workType);
        }
    }
}
